﻿using OurMemory.Model.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OurMemory
{
    internal class HappyMemoriesServices
    {
        /// <summary>
        /// 获得所有数据
        /// </summary>
        /// <returns></returns>
        public List<HappyMemories> GetAll()
        {
            var db = new OurMemoryEntities();
            return db.HappyMemories.ToList();
        }
        /// <summary>
        /// 新建记录
        /// </summary>
        /// <param name="model"></param>
        public void Create(HappyMemoriesCreateModel model)
        {
            var db = new OurMemoryEntities();
            var newItem = new HappyMemories()
            {
                Id = Guid.NewGuid(),
                Story = model.Story,
                Date = model.Date,
                Photo = model.Photo,
                Recorder = model.Recorder,
                Feelings = model.Feelings,
            };
            db.HappyMemories.Add(newItem);
            db.SaveChanges();
        }
        /// <summary>
        /// 根据Id，获得指定记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HappyMemories GetById(Guid id)
        {
            var db = new OurMemoryEntities();
            return db.HappyMemories.Find(id);
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="model"></param>
        public void Edit(HappyMemoriesEditModel model)
        {
            var db = new OurMemoryEntities();
            var item = db.HappyMemories.Find(model.Id);
            if (item != null)
            {
                item.Story= model.Story;
                item.Date = model.Date;
                item.Photo = model.Photo;
                item.Recorder = model.Recorder;
                item.Feelings = model.Feelings;
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(Guid id)
        {
            var db = new OurMemoryEntities();
            var item = db.HappyMemories.Find(id);
            if (item != null)
            {
                db.HappyMemories.Remove(item);
                db.SaveChanges();
            }
        }
    }
}
